package gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.LinkedList;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.SwingWorker;

import mail.Mail;
import mail.MyModelEmail;
import tools.OptionReader;
import calendar.CheckboxCalendar;

/**
 * 
 * @author Michael Weng
 * 
 *         Fenster für einen neuen Termin.
 * 
 */

public class NewEventWindow extends JFrame {

	/**
	 * Name Start Ende Ort Teilnehmer (Zeile mit +Button) Kalender Notiz
	 */

	private static final long serialVersionUID = 968579331899246093L;
	@SuppressWarnings("unused")
	private Date tmp;
	private Connection connection = null;

	// Textfelder fuer Detailangaben zu jeweiligen Tasks
	private JTextField textField;
	private JTextField textField_1;
	private JTextField textField_2;
	private Test mainInstance;
	private Statement stat = null;
	private int idEvents = 0;
	@SuppressWarnings("rawtypes")
	private LinkedList participants = new LinkedList();
	private LinkedList<CheckboxCalendar> cals = new LinkedList<CheckboxCalendar>();
	private OptionReader or;
	private String[] windowColorsSeperated;
	private int r;
	private int g;
	private int b;
	private String[] calDropdown;
	private JComboBox kalender;
	private LinkedList<String> emailLinkedList;

	private JTextArea textArea;

	private JFrame temp;

	// Sprache
	int speak = 0;

	String[] titel = { "Titel", "heading", "titulus" };
	String[] begin = { "Begin", "begin", "initium" };
	String[] ende = { "Ende", "end", "finis" };
	String[] ort = { "Ort", "location", "locus" };
	String[] teilnehmer = { "Teilnehmer", "participants", "auditor" };
	String[] kalenderR = { "Kalender", "calendar", "calendarium" };
	String[] notiz = { "Notiz", "note", "hypomnema" };

	String[] kalenderRR = { "Kalender", "Calendar", "Calendarium" };
	String[] kontakte = { "Kontakte", "Contacts", "Contactus" };
	String[] todo = { "Aufgabe", "Task", "Negotium" };
	String[] mail = { "E-Mail", "Mail", "Littersa" };

	String[] terminhinzu = { "Termin Hinzufügen", "Add Event",
			"Eventuum Addere" };
	String[] uhr = { "Uhr", "", "" };

	String[] endevorbegin = { "Ende des Termins liegt vor dem Begin.",
			"End of the event is prior to the Begin.",
			"Initium finis est prior quam res." };
	String[] unbekannterfehler = { "Unbekannter Fehler.", "Unknown Error.",
			"Ignoto erroris est." };

	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					NewEventWindow frame = new NewEventWindow();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});

	}

	/**
	 * ardkonstruktor ohne Date
	 */

	public NewEventWindow() {

		// nimmt das aktuelle Datum
		Date datumAktuell = new Date();

		init(datumAktuell, null, null);

	}

	/**
	 * Konstruktor mit Datum
	 * 
	 * @param d
	 *            Datum das gesetzt werden soll
	 */
	public NewEventWindow(Date d) {

		init(d, null, null);

	}

	/**
	 * Konstruktor der die Instanz von CalView mitbekommt
	 * 
	 * @param cv
	 *            Instanz der Monatsansicht
	 */
	public NewEventWindow(CalView cv) {

		// nimmt das aktuelle Datum
		Date datumAktuell = new Date();

		init(datumAktuell, cv, null);

	}

	/**
	 * Konstruktor mit Date uebergabe und Instanz von CalView
	 * 
	 * @param d
	 *            der vorgewählt sein soll
	 * @param cv
	 *            Instanz der Moantsansicht
	 */

	public NewEventWindow(Date d, CalView cv) {
		init(d, cv, null);
	}

	/**
	 * Konstruktor
	 * 
	 * @param d
	 *            Tag der vorgewählt sein soll
	 * @param wv
	 *            Instanz der Wochenansicht
	 */
	public NewEventWindow(Date d, WeekView wv) {
		init(d, null, wv);
	}

	/**
	 * Init Funktion mit Standartparametern
	 * 
	 * @param d
	 *            Tag der gesetzt wird
	 * @param cv
	 *            Instanz der Monatsansicht
	 * @param wv
	 *            Instanz der Wochenansicht
	 */
	@SuppressWarnings("deprecation")
	public void init(final Date d, final CalView cv, final WeekView wv) {
		if (cv != null) {
			tmp = d;
			this.mainInstance = cv.getMainInstance();

		}

		if (wv != null) {
			tmp = d;
			this.mainInstance = wv.getMainInstance();
		}

		or = new OptionReader();
		try {
			or.getOpionsFromFile();
		} catch (IOException e2) {

			e2.printStackTrace();
		}

		String windowColors = or.getColorSetting();
		windowColorsSeperated = windowColors.split(",");
		r = Integer.parseInt(windowColorsSeperated[0]);
		g = Integer.parseInt(windowColorsSeperated[1]);
		b = Integer.parseInt(windowColorsSeperated[2]);

		speak = Integer.parseInt(or.getLanguage());

		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		Dimension size = new Dimension(700, 500);
		setSize(size);
		setPreferredSize(size);
		setMinimumSize(size);
		setMaximumSize(size);

		setTitle(terminhinzu[speak]);
		getContentPane().setBackground(Color.LIGHT_GRAY);
		getContentPane().setLayout(new BorderLayout(0, 0));
		try {
			Class.forName("com.mysql.jdbc.Driver");
			connection = DriverManager
					.getConnection("jdbc:mysql://win9142.informatik.uni-wuerzburg.de:3306?user=awesomefireballs&password=jesus");
		} catch (ClassNotFoundException e) {

			e.printStackTrace();
		} catch (SQLException e) {

			e.printStackTrace();

		}
		try {
			stat = connection.createStatement();
			stat.executeQuery("use `sopra-ws12-awesome_fireballs`;");

			ResultSet party = stat
					.executeQuery("SELECT idEvents FROM `sopra-ws12-awesome_fireballs`.`Events`ORDER BY idEvents DESC LIMIT 1;");

			while (party.next()) {
				idEvents = party.getInt(1) + 1;
			}

			ResultSet idsCalendar = stat
					.executeQuery("SELECT idCalendar, name, idCreator FROM Calendar WHERE idUser = "
							+ Test.iduser + ";");

			while (idsCalendar.next()) {
				CheckboxCalendar temp = new CheckboxCalendar(
						idsCalendar.getInt("idCalendar"),
						idsCalendar.getString("name"),
						idsCalendar.getInt("idCreator"));
				cals.add(temp);
			}

			calDropdown = new String[cals.size()];
			for (int i = 0; i < cals.size(); i++) {
				calDropdown[i] = cals.get(i).getName();
			}

		} catch (SQLException e1) {
			e1.printStackTrace();
		}

		// Variablen fuer das Datum
		int y = d.getYear() + 1900;
		int m = d.getMonth() + 1;
		int t = d.getDate();
		// Rechts Panel, welches Detailansicht enthaelt
		JPanel centerpanel = new JPanel();
		centerpanel.setBackground(new Color(r, g, b));
		getContentPane().add(centerpanel, BorderLayout.CENTER);
		centerpanel.setLayout(null);

		// Erstellen der Label zu jeweiligen Textfeldern
		JLabel lblName = new JLabel(titel[speak]);
		lblName.setHorizontalAlignment(SwingConstants.RIGHT);
		lblName.setBounds(30, 30, 97, 16);
		centerpanel.add(lblName);

		JLabel lblBegin = new JLabel(begin[speak]);
		lblBegin.setHorizontalAlignment(SwingConstants.RIGHT);
		lblBegin.setBounds(30, 60, 97, 16);
		centerpanel.add(lblBegin);

		JLabel lblEnde = new JLabel(ende[speak]);
		lblEnde.setHorizontalAlignment(SwingConstants.RIGHT);
		lblEnde.setBounds(30, 90, 97, 16);
		centerpanel.add(lblEnde);

		JLabel lblOrt = new JLabel(ort[speak]);
		lblOrt.setHorizontalAlignment(SwingConstants.RIGHT);
		lblOrt.setBounds(30, 120, 97, 16);
		centerpanel.add(lblOrt);

		JLabel lblTnm = new JLabel(teilnehmer[speak]);
		lblTnm.setHorizontalAlignment(SwingConstants.RIGHT);
		lblTnm.setBounds(30, 154, 97, 16);
		centerpanel.add(lblTnm);

		JLabel lblCld = new JLabel(kalenderR[speak]);
		lblCld.setHorizontalAlignment(SwingConstants.RIGHT);
		lblCld.setBounds(30, 186, 97, 16);
		centerpanel.add(lblCld);

		JLabel lblNtz = new JLabel(notiz[speak]);
		lblNtz.setHorizontalAlignment(SwingConstants.RIGHT);
		lblNtz.setBounds(30, 214, 97, 16);
		centerpanel.add(lblNtz);

		// Erstellen der Textfelder

		// Titel
		textField = new JTextField();
		textField.setBounds(160, 24, 430, 28);
		centerpanel.add(textField);
		textField.setColumns(10);

		// Ort
		textField_1 = new JTextField();
		textField_1.setBounds(160, 114, 344, 28);
		centerpanel.add(textField_1);
		textField_1.setColumns(10);

		// Teilnehmer
		textField_2 = new JTextField();
		textField_2.setBounds(160, 148, 309, 28);
		centerpanel.add(textField_2);
		textField_2.setEditable(false);
		textField_2.setColumns(10);

		// Button Teilnehmer Hinzufuegen
		JButton btnAddParticipants = new JButton("+");
		btnAddParticipants.setBounds(471, 150, 33, 27);
		centerpanel.add(btnAddParticipants);

		temp = this;

		btnAddParticipants.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {

				JFrame addcontact = new AddContactWindow(temp, false);
				addcontact.setVisible(true);

			}
		});

		// Kalender
		kalender = new JComboBox(calDropdown);

		kalender.setBounds(160, 182, 344, 28);

		centerpanel.add(kalender);

		// Details
		textArea = new JTextArea();
		textArea.setLineWrap(true);
		textArea.setWrapStyleWord(true);
		JScrollPane textAreaScrollPane = new JScrollPane(textArea);

		textAreaScrollPane.setBounds(160, 218, 430, 180);
		// textArea.setBounds(160, 218, 430, 180);
		centerpanel.add(textAreaScrollPane);

		// Erstellen der Buttons
		JButton btnSpeichern = new JButton(terminhinzu[speak]);
		btnSpeichern.setBounds(440, 410, 158, 29);
		centerpanel.add(btnSpeichern);

		// ///////// ComboBoxen fuer Begin

		// Jahr //

		String[] jahreArray = new String[30];

		int jz = (new Date().getYear()) + 1900;
		for (int i = 0; i < 30; i++) {
			jahreArray[i] = Integer.toString(jz);
			jz++;
		}

		final JComboBox jahr = new JComboBox(jahreArray);
		// Test ob date in Vergangenheit ist

		int pos = (y - (new Date().getYear() + 1900));

		if (pos < 0) {
			jahr.setSelectedIndex(0);
		} else {
			jahr.setSelectedIndex(pos);
		}
		// !
		jahr.setBounds(310, 56, 85, 20);
		centerpanel.add(jahr);

		// Monat //

		String[] monatsArray = new String[12];

		for (int i = 0; i < 12; i++) {
			String s = "";
			if (i < 9) {
				s = "0";
			}
			s += Integer.toString(i + 1);
			monatsArray[i] = s;
		}

		final JComboBox monat = new JComboBox(monatsArray);
		monat.setSelectedIndex(m - 1);

		monat.setBounds(235, 56, 70, 20);
		centerpanel.add(monat);

		// Tag //

		String[] tagesArray = new String[31];

		for (int i = 0; i < 31; i++) {
			String s = "";
			if (i < 9) {
				s = "0";
			}
			s += Integer.toString(i + 1);
			tagesArray[i] = s;
		}

		final JComboBox tag = new JComboBox(tagesArray);
		tag.setSelectedIndex(t - 1);

		tag.setBounds(160, 56, 70, 20);
		centerpanel.add(tag);

		// Labels vor Tag und nach Uhrzeit

		JLabel lblUhr = new JLabel(uhr[speak]);
		lblUhr.setHorizontalAlignment(SwingConstants.RIGHT);
		lblUhr.setBounds(550, 56, 40, 20);
		centerpanel.add(lblUhr);

		// Stunde //

		String[] stundenArray = new String[24];

		for (int i = 0; i < 24; i++) {

			stundenArray[i] = Integer.toString(i);
		}

		final JComboBox stunde = new JComboBox(stundenArray);

		stunde.setSelectedIndex(8);

		stunde.setBounds(420, 56, 70, 20);
		centerpanel.add(stunde);

		// .getSelectedItem()

		// Minute //

		String[] minutenArray = new String[4];

		minutenArray[0] = "00";
		minutenArray[1] = "15";
		minutenArray[2] = "30";
		minutenArray[3] = "45";

		final JComboBox minute = new JComboBox(minutenArray);

		minute.setBounds(495, 56, 70, 20);
		centerpanel.add(minute);

		// ///////// ComboBoxen fuer Ende ///////////////////////
		// Gleiche Variablen namen nur immer mit einem "n" davor

		// Jahr //

		final JComboBox njahr = new JComboBox(jahreArray);

		if (pos < 0) {
			njahr.setSelectedIndex(0);
		} else {
			njahr.setSelectedIndex(pos);
		}

		njahr.setBounds(310, 86, 85, 20);
		centerpanel.add(njahr);

		// Monat //

		final JComboBox nmonat = new JComboBox(monatsArray);
		nmonat.setSelectedIndex(m - 1);

		nmonat.setBounds(235, 86, 70, 20);
		centerpanel.add(nmonat);

		// Tag //

		final JComboBox ntag = new JComboBox(tagesArray);
		ntag.setSelectedIndex(t - 1);

		ntag.setBounds(160, 86, 70, 20);
		centerpanel.add(ntag);

		// Labels vor Tag und nach Uhrzeit

		JLabel nlblUhr = new JLabel(uhr[speak]);
		nlblUhr.setHorizontalAlignment(SwingConstants.RIGHT);
		nlblUhr.setBounds(550, 86, 40, 20);
		centerpanel.add(nlblUhr);

		// Stunde //

		final JComboBox nstunde = new JComboBox(stundenArray);

		nstunde.setSelectedIndex(12);

		nstunde.setBounds(420, 86, 70, 20);
		centerpanel.add(nstunde);

		// .getSelectedItem()

		// Minute //

		final JComboBox nminute = new JComboBox(minutenArray);

		nminute.setBounds(495, 86, 70, 20);
		centerpanel.add(nminute);

		// /// BUTTON ACTION /////

		ActionListener bu = new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				if (cv != null) {
					cv.getMainInstance().setProgressBarVisibleTrue();
				}

				if (wv != null) {
					wv.getMainInstance().setProgressBarVisibleTrue();
				}

				@SuppressWarnings("rawtypes")
				SwingWorker worker = new SwingWorker() {

					@Override
					public Object doInBackground() {

						try {
							Class.forName("com.mysql.jdbc.Driver");
							connection = DriverManager
									.getConnection("jdbc:mysql://win9142.informatik.uni-wuerzburg.de:3306?user=awesomefireballs&password=jesus");
						} catch (ClassNotFoundException e) {

							e.printStackTrace();
						} catch (SQLException e) {

							e.printStackTrace();
							return false;
						}

						try {

							// / Baut die Dates ///

							// / Begin
							int beginY = Integer.parseInt((String) jahr
									.getSelectedItem());
							int beginM = monat.getSelectedIndex() + 1;
							int beginD = tag.getSelectedIndex() + 1;
							int beginH = stunde.getSelectedIndex();
							int beginMin = Integer.parseInt((String) minute
									.getSelectedItem());

							Date begin = makeDate(beginY, beginM, beginD,
									beginH, beginMin);

							// / End
							int nbeginY = Integer.parseInt((String) njahr
									.getSelectedItem());
							int nbeginM = nmonat.getSelectedIndex() + 1;
							int nbeginD = ntag.getSelectedIndex() + 1;
							int nbeginH = nstunde.getSelectedIndex();
							int nbeginMin = Integer.parseInt((String) nminute
									.getSelectedItem());

							Date end = makeDate(nbeginY, nbeginM, nbeginD,
									nbeginH, nbeginMin);

							int selectedCal = kalender.getSelectedIndex();
							int idSelectedCal = cals.get(selectedCal)
									.getIdCalendar();

							// / Test ob Ende nach Begin liegt
							if ((testMonth(beginY, beginM, beginD, beginH,
									beginMin) == false)
									|| (testMonth(nbeginY, nbeginM, nbeginD,
											nbeginH, nbeginMin) == false)) {

							}

							else if (end.before(begin)) {

								new NotifyFrame(endevorbegin[speak])
										.setVisible(true);

							}

							else {

								// Dates in einen String der korrekten Form
								// bringen.
								SimpleDateFormat sdf = new SimpleDateFormat(
										"yyyy-MM-dd HH:mm:ss");

								StringBuilder sBBegin = new StringBuilder(
										sdf.format(begin));

								StringBuilder sBEnd = new StringBuilder(
										sdf.format(end));

								String beginString = sBBegin.toString();
								String endString = sBEnd.toString();

								// Liest die Werte aus

								String name = textField.getText();
								String ort = textField_1.getText();
								String notiz = textArea.getText();

								/*
								 * ResultSet calendar = stat .executeQuery(
								 * "SELECT idCalendar FROM `sopra-ws12-awesome_fireballs`.Caldendar ORDER BY idCalendar DESC LIMIT 1;"
								 * ); int idCalendar = 0; while
								 * (calendar.next()) { idCalendar =
								 * calendar.getInt(1) + 1; }
								 */
								stat.executeQuery("use `sopra-ws12-awesome_fireballs`;");
								connection.setAutoCommit(false);
								PreparedStatement pstmt = connection
										.prepareStatement("INSERT INTO `sopra-ws12-awesome_fireballs`.Events VALUES(?, ?, ?, ?, ?, ?, ?)");

								pstmt.setInt(1, idEvents);
								// Diese 2 mit Kalender ersetzen
								pstmt.setInt(2, idSelectedCal);

								pstmt.setString(3, name);
								pstmt.setString(4, beginString);
								pstmt.setString(5, endString);
								pstmt.setString(6, ort);
								pstmt.setString(7, notiz);
								pstmt.addBatch();
								System.out.println(pstmt.toString());

								ResultSet nextparticipantid = stat
										.executeQuery("SELECT idparticipants FROM `sopra-ws12-awesome_fireballs`.participants ORDER BY idparticipants DESC LIMIT 1;");
								int nextpartid = 0;
								while (nextparticipantid.next()) {
									nextpartid = nextparticipantid.getInt(1) + 1;
								}
								// ppstmt = Prepared Participants STateMenT
								PreparedStatement ppstmt = connection
										.prepareStatement("INSERT INTO `sopra-ws12-awesome_fireballs`.participants VALUES(?, ?, ?)");
								System.out.println(participants.size());
								for (int i = 0; i < participants.size(); i++) {
									ppstmt.setInt(1, nextpartid + i);
									ppstmt.setInt(2, idEvents);
									ppstmt.setString(3,
											(String) participants.get(i));
									ppstmt.addBatch();

								}

								pstmt.executeBatch();
								ppstmt.executeBatch();
								connection.commit();
								connection.setAutoCommit(true);

							}
						} catch (SQLException e) {
							e.printStackTrace();
						}

						return stat;
					}

					@Override
					public void done() {
						// TODO
						if (emailLinkedList != null) {
							if (emailLinkedList.size() > 0) {
								Mail temp = new Mail();

								temp.setTo(emailLinkedList);

								temp.setBetreff("Einladung zu Termin: "
										+ textField.getText());

								temp.setText(idEvents + "");

								MyModelEmail mme = new MyModelEmail();
								mme.sendMail(temp, true);
							}
						}
						dispose();

					}

				};

				worker.execute();
				if (cv != null) {

					mainInstance.getTabbedPane().removeTabAt(0);
					mainInstance.setKalender(new CalView(mainInstance));
					mainInstance.getTabbedPane().add(
							mainInstance.getKalender(), kalenderRR[speak]);

					mainInstance.getTabbedPane().remove(
							mainInstance.getKontakte());
					mainInstance.setKontakte(new AddContactPane());
					mainInstance.getTabbedPane().add(
							mainInstance.getKontakte(), kontakte[speak]);

					mainInstance.getTabbedPane().remove(mainInstance.getToDo());
					mainInstance.setToDo(new ToDoPane());
					mainInstance.getTabbedPane().add(mainInstance.getToDo(),
							todo[speak]);

					mainInstance.getTabbedPane().remove(
							mainInstance.getViewMail());
					mainInstance.setViewMail(new ViewMail(mainInstance));
					mainInstance.getTabbedPane().add(
							mainInstance.getViewMail(), mail[speak]);

					cv.getMainInstance().setProgressBarVisibleFalse();
				}

				if (wv != null) {

					mainInstance.getTabbedPane().removeTabAt(0);
					mainInstance.setKalender(new WeekView(mainInstance, d));
					mainInstance.getTabbedPane().add(
							mainInstance.getKalender(), kalenderRR[speak]);

					mainInstance.getTabbedPane().remove(
							mainInstance.getKontakte());
					mainInstance.setKalender(new AddContactPane());
					mainInstance.getTabbedPane().add(
							mainInstance.getKontakte(), kontakte[speak]);

					mainInstance.getTabbedPane().remove(mainInstance.getToDo());
					mainInstance.setKalender(new ToDoPane());
					mainInstance.getTabbedPane().add(mainInstance.getToDo(),
							todo[speak]);

					mainInstance.getTabbedPane().remove(
							mainInstance.getViewMail());
					mainInstance.setKalender(new ViewMail(mainInstance));
					mainInstance.getTabbedPane().add(
							mainInstance.getViewMail(), mail[speak]);

					wv.getMainInstance().setProgressBarVisibleFalse();
				}

			}

		};

		btnSpeichern.addActionListener(bu);

	}

	// Macht aus den Angaben ein gueltiges Date.
	/**
	 * Erzeugt gültiges Datum
	 * 
	 * @param year
	 * @param month
	 * @param day
	 * @param hour
	 * @param minute
	 * @return
	 */
	@SuppressWarnings("deprecation")
	public Boolean testMonth(int year, int month, int day, int hour, int minute) {
		// Date datumAktuell = new Date();
		//
		//
		// // Variablen fuer das Datum
		// int aktuJahr = datumAktuell.getYear() + 1900;
		// int m = datumAktuell.getMonth() + 1;
		// int t = datumAktuell.getDate();
		// int h = datumAktuell.getHours();

		// Prueft ob ints Sinn ergeben
		if (year < 1900 || month < 1 || month > 12 || day < 1 || day > 31
				|| hour < 0 || hour > 23 || minute < 0 || minute > 45) {

			new NotifyFrame(unbekannterfehler[speak]).setVisible(true);
			return false;

		}

		// Prueft ob es den Tag in diesem Monat gibt.
		else if (day > monthLength(new Date(year - 1900, month - 1, 1))) {

			new NotifyFrame("Dieser Monat besitzt diesen Tag nicht.")
					.setVisible(true);
			return false;
		}

		else {
			return true;
		}

	}

	@SuppressWarnings("deprecation")
	public static Date makeDate(int year, int month, int day, int hour,
			int minute) {

		return new Date(year - 1900, month - 1, day, hour, minute);

	}

	// Ermittelt Laenge des Monats.

	@SuppressWarnings("deprecation")
	public static int monthLength(Date d) {

		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.YEAR, d.getYear());
		cal.set(Calendar.MONTH, d.getMonth());
		cal.set(Calendar.DATE, d.getDate());

		return cal.getActualMaximum(Calendar.DAY_OF_MONTH);
	}

	public int getIdEvents() {
		return idEvents;
	}

	public void setTextTeilnehmer(String s) {
		textField_2.setText(s);
	}

	/**
	 * Erzeugt dass Namen im An und CC Feld stehen statt Email adressen
	 */
	public void setParticipantsText() {
		emailLinkedList = new LinkedList<String>();
		String s = "";
		String mail = "";
		Statement stat;
		try {
			stat = connection.createStatement();

			stat.executeQuery("use `sopra-ws12-awesome_fireballs`;");

			for (int i = 0; i < participants.size(); i++) {
				ResultSet rs = stat
						.executeQuery("SELECT Adress.* FROM Adress WHERE idAdress= "
								+ participants.get(i) + ";");
				rs.next();
				s += rs.getString("fname") + " ";
				s += rs.getString("lname") + ", ";
				mail = rs.getString("email");
				emailLinkedList.add(mail);

			}
		} catch (SQLException e) {
			e.printStackTrace();
		}

		textField_2.setText(s);
	}

	@SuppressWarnings("rawtypes")
	public void setParticipants(LinkedList ll) {
		this.participants = ll;
	}

}
